package com.kaka.bus.engine.home.info.record;

import com.kaka.bus.bean.HttpWrapper;
import com.kaka.bus.bean.MoneyRecordBean;
import com.kaka.bus.di.DataManager;
import com.kaka.bus.manager.Result;
import com.kaka.bus.manager.T;
import com.kaka.bus.mvp.RxPresenter;
import com.kaka.bus.util.U;

import java.util.List;

import javax.inject.Inject;

/**
 * Created by zhangqi on 2016/11/7.
 */

public class RecordPresenter extends RxPresenter<RecordContract.View> implements RecordContract.Presenter {

    @Inject
    public RecordPresenter(DataManager dataManager) {
        super(dataManager);
    }

    @Override
    public void initData(String userId, String token, int page, int pageSize) {
        request(userId,token,page,pageSize,false);
    }

    @Override
    public void loadMore(String userId, String token, int page, int pageSize) {
        request(userId,token,page,pageSize,true);
    }

    private void request(String userId, String token, int page, int pageSize, boolean isLoadMore){
        addSubscribe(mDataManager.getApi()
                .perAccountLog(userId,page,pageSize,token, U.Sign(userId,page+"",pageSize+"",token))
                .compose(T.D())
                .subscribeWith(new Result<HttpWrapper<List<MoneyRecordBean>>>() {
                    @Override
                    protected void onSuccess(HttpWrapper<List<MoneyRecordBean>> data) {
                        if(isLoadMore){
                            mView.onResultLoadMore(data);
                        }else{
                            mView.onResultStart(data);
                        }
                    }

                    @Override
                    protected void onFaild() {
                        if(isLoadMore){
                            mView.onError();
                        }
                    }
                }));
    }
}
